package com.xbsg.protocol.xf.entiy;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

public class AppDataUtil {
    public static String toASII(String body){
        StringBuilder result = new StringBuilder();
        //part_address
        String[] strs1 = body.split(" ");
        for(String str :strs1){
            char tmp = (char)Integer.parseInt(str);
            result.append(tmp);
        }
        return result.toString();
    }
    //字节数组转 GB18030
    public static String gB18030ByteArrayToUtf8String(byte[] bytes) {
        ByteBuffer byteBuffer = ByteBuffer.wrap(bytes);
        CharBuffer gb18030 = Charset.forName("GB18030").decode(byteBuffer);
        ByteBuffer utf8 = Charset.forName("UTF8").encode(gb18030);
        byte[] buffer = utf8.array();
        int length = 0;
        for (int i = 0; i < buffer.length; ++i) {
            if (buffer[i] == 0) {
                length = i;
                break;
            }
        }
        return new String(buffer, 0, length);
    }
    //10进制字符串转字节数组
    public static byte[] hexStringToBytes(String body,int radix){
        String[] strs1 = body.split(" ");
        byte[] bytes = new byte[strs1.length];
        for(int i =0;i<strs1.length;i++){
            //(byte) Integer.parseInt(hex.substring(i * 2, i * 2 + 2), 16);
            //十进制转字节
            bytes[i] = (byte)Integer.parseInt(strs1[i], 10);
        }
        return bytes;
    }

    public static Map<String,String> data_flag_map = new HashMap();
    public static Map<String,String> device_type_map = new HashMap<>();
    public static Map<String,String> part_type_map = new HashMap<>();
    public static Map<String,String> device_part_status_map = new HashMap<>();

    public static Map<String,String> device_part_opt_map = new HashMap<>();
    static {
        data_flag_map.put("2","上传火灾自动报警系统部件运行状态");
        data_flag_map.put("4","上传火灾自动报警系统设备操作信息");
        data_flag_map.put("9","通信线路上行测试");
        //data_flag_map.put()
        device_type_map.put("0","预留");
        device_type_map.put("1","火灾报警控制器/消防联动控制器");
        device_type_map.put("2","消防控制室图形显示装置");
        device_type_map.put("10","可燃气体报警控制器");
        device_type_map.put("11","电气火灾监控设备");
        device_type_map.put("20","消火栓系统");
        device_type_map.put("21","自动喷水灭火系统设备、水喷雾灭火系统设备");
        device_type_map.put("22","气体灭火控制器");
        device_type_map.put("23","泡沫灭火系统设备");
        device_type_map.put("24","干粉灭火系统设备");
        device_type_map.put("25","防烟排烟系统设备");
        device_type_map.put("26","防火门及卷帘系统设备");
        device_type_map.put("27","消防应急广播");
        device_type_map.put("28","消防电话");
        device_type_map.put("29","消防应急照明和疏散指示系统设备");
        device_type_map.put("30","消防电源");

        part_type_map.put("78","点型光电感烟火灾探测器");
        part_type_map.put("123","输入/输出模块");
        part_type_map.put("121","输入模块");
        part_type_map.put("122","输出模块");

        device_part_status_map.put("1","火警（可燃气体、电气火灾报警）");
        device_part_status_map.put("2","低限报警");
        device_part_status_map.put("3","高限报警");
        device_part_status_map.put("4","超量程报警");
        device_part_status_map.put("5","剩余电流报警");
        device_part_status_map.put("60","启动");
        device_part_status_map.put("70","停止");
        device_part_status_map.put("80","反馈");
        device_part_status_map.put("82","反馈撤销");
        device_part_status_map.put("21","通讯故障");
        device_part_status_map.put("41","通讯故障恢复");
      /*     device_part_status_map.put("5","通讯故障");
        device_part_status_map.put("5","通讯故障恢复");*/

        device_part_status_map.put("6","温度报警");
        device_part_status_map.put("7","电弧报警");
        device_part_status_map.put("8","感烟探测器报警");
        device_part_status_map.put("9","感温探测器报警");
        device_part_status_map.put("10","手动报警按钮报警");

        device_part_opt_map.put("0","无操作");
        device_part_opt_map.put("1","复位");
        device_part_opt_map.put("2","消音");
        device_part_opt_map.put("3","手动报警");
        device_part_opt_map.put("4","屏蔽");
        device_part_opt_map.put("5","屏蔽解除");
    }

    public static AppData parseAppData(String message){
        AppData appData = new AppData();
        String[] temps = message.split(" ");
        appData.type_flag = temps[0];
        appData.object_num = temps[1];
        if("2".equals(appData.type_flag)){
            //部件运行状态，44
            int size = Integer.parseInt(appData.object_num);
            appData.status_objects = new ArrayList<>(size);
            for(int j =0;j<size;j++){
                AppData.StatusInfoObject infoObject = new AppData.StatusInfoObject();
                //appData.status_objects.get(j);
                infoObject.device_type = temps[2 +j*44];
                infoObject.device_address = temps[3+j*44] ;
                infoObject.part_type = temps[4+j*44];
                for(int i =5 +j*44;i<13 +j*44;i++){
                    infoObject.part_address += temps[i] + " ";
                }
                infoObject.part_status = temps[13 +j*44];
                for(int i =14 +j*44;i<46 +j*44;i++){
                    infoObject.part_desc += temps[i] + " ";
                }

/*                for(int i = 46 +j*44;i<46 + 6 +j*44 ;i++){
                    infoObject.time_label += temps[i]+" ";
                }*/
                for(int i = 46 + 5 +j*44;i>=46 + j*44;i--){
                    infoObject.time_label += temps[i]+" ";
                }
                appData.status_objects.add(infoObject);
            }

        }else if("4".equals(appData.type_flag)){
            //设备操作状态
            AppData.OptInfoObject optInfoObject = new AppData.OptInfoObject();
            optInfoObject.device_type= temps[2];
            optInfoObject.device_address = temps[3] ;
            optInfoObject.opt_type = temps[4] ;
            optInfoObject.opter = temps[5] ;
            for(int i = 6 +6 -1;i>=6;i--){
                optInfoObject.time_label += temps[i]+" ";
            }

            appData.optInfoObject = optInfoObject;
        }else if("9".equals(appData.type_flag)){
            //System.out.println("心跳信息");
        }
        return appData;
    }

    public static String parse_time_stamp(String timeStamp){
        if(isNotZero(timeStamp)){
            StringBuilder result = new StringBuilder("20");
            String[] strs1 = timeStamp.split(" ");
            for(int i =0;i<strs1.length;i++){
                result.append(strs1[i]);
                if(i<2){
                    result.append("-");
                }else if(i ==2){
                    result.append(" ");
                }else if(i>2 && i<strs1.length-1){
                    result.append(":");
                }
            }
            return result.toString();
        }else {
            return "0";
        }
    }

    private static boolean isNotZero(CharSequence cs){
        int strLen = cs == null ? 0:cs.length();
        if(strLen == 0){
            return false;
        }else {
            for(int i =0;i<strLen;++i){
                if(cs.charAt(i) !='0' && !Character.isWhitespace(cs.charAt(i))){
                    return true;
                }
            }
        }
        return false;
    }

}
